define [
  'views/base/view'
  'views/base/form_view'
  'text!templates/user/login.html'
], (View, FormView, template) ->

  'use strict'

  class LoginView extends View

    # Override Attributtes
    # =============================================================

    template   : template
    autoRender : true
    id         : 'login-view'

    events:
      'submit #form-login'         : 'submitFormHandler'

    # Override Methods
    # =============================================================

    cacheDOMElements: ->
      @DOMElements =
        $formLogin     : @$ '#form-login'
        $username      : @$ '#username'
        $password      : @$ '#password'

    afterRender: ->
      super

      @DOMElements.$formLogin.validate
        rules:
          username:
            required : true
          password:
            required  : true

    disableActions : ->
      $el = @$form or @$el
      if $el then $el.find('button, a, .btn, select, input, i, textarea').attr('disabled', true).addClass('disabled')

    enableActions:->
      $el = @$form or @$el
      if $el then $el.find('button, a, .btn, select, input, i, textarea').removeAttr('disabled').removeClass('disabled')
      @DOMElements.$formLogin.find('button[type="submit"]').text 'Continuar'

    # Aditional Methods
    # =============================================================

    # Event Handlers
    # =============================================================

    submitFormHandler: (event) ->
      event.preventDefault()

      formData =
        username      : @DOMElements.$username.val().toLowerCase()
        password   : @DOMElements.$password   .val()

      @DOMElements.$formLogin.find('button[type="submit"]').text 'Verificando credênciais...'
      @disableActions()
      @trigger 'signin', formData